﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AAA.ResultSet
{
    public class DefaultResultSetGroup : IResultSet, IResultSetGroup
    {
        private Dictionary<string, IResultSet> _dicResultSet;
        private Dictionary<string, object> _dicFormat;
        private List<string> _lstGroupNames;
        private string _strActiveResultSet;
        private Dictionary<string, string> _dicAttributes;

        public DefaultResultSetGroup()
        {
            _dicResultSet = new Dictionary<string, IResultSet>();
            _dicFormat = new Dictionary<string, object>();
            _lstGroupNames = new List<string>();
            _dicAttributes = new Dictionary<string, string>();
        }

        #region IResultSetGroup Members

        public int Count
        {
            get
            {
                return _dicResultSet.Keys.Count;
            }
        }

        public IResultSet GetResult(int iIndex)
        {
            try
            {
                return GetResult(_lstGroupNames[iIndex]);
            }
            catch (Exception ex)
            {
            }
            return null;
        }

        public IResultSet GetResult(string strName)
        {
            if (_dicResultSet.ContainsKey(strName))
                return _dicResultSet[strName];
            return null;
        }

        public object GetFormat(int iIndex)
        {
            try
            {
                return GetFormat(_lstGroupNames[iIndex]);
            }
            catch (Exception ex)
            {
            }
            return null;
        }

        public object GetFormat(string strName)
        {
            if (_dicFormat.ContainsKey(strName))
                return _dicFormat[strName];
            return null;
        }


        public void AddResultSet(string strName, IResultSet resultSet, object oFormat)
        {
            if (_dicResultSet.ContainsKey(strName))
            {
                _dicResultSet[strName] = resultSet;
                _dicFormat[strName] = oFormat;
            }
            else
            {
                _dicResultSet.Add(strName, resultSet);
                _dicFormat.Add(strName, oFormat);
            }
        }

        public string ActiveResultSet
        {
            get
            {
                return _strActiveResultSet;
            }
            set
            {
                _strActiveResultSet = value;
            }
        }

        public List<string> ResultNames()
        {
            List<string> lstResultNames = new List<string>();

            foreach (string strName in _dicResultSet.Keys)
                lstResultNames.Add(strName);
            return lstResultNames;
        }

        #endregion
        #region IResultSet Members

        public bool Load()
        {
            foreach (IResultSet resultSet in _dicResultSet.Values)
                if (!resultSet.Load())
                    return false;
            return true;
        }

        public void SetAttribute(string strAttribute, string strValue)
        {
            if (_dicAttributes.ContainsKey(strAttribute))
                _dicAttributes[strAttribute] = strValue;
            else
                _dicAttributes.Add(strAttribute, strValue);
        }

        public string GetAttribute(string strAttribute)
        {
            return _dicAttributes.ContainsKey(strAttribute)
                    ? _dicAttributes[strAttribute]
                    : null;
        }

        public bool Read()
        {
            return _dicResultSet.ContainsKey(ActiveResultSet)
                    ?   _dicResultSet[ActiveResultSet].Read()
                    :   false;
        }

        public Encoding Encoding
        {
            get
            {
                return _dicResultSet.ContainsKey(ActiveResultSet)
                        ? _dicResultSet[ActiveResultSet].Encoding
                        :   null;
            }
            set
            {
                if(_dicResultSet.ContainsKey(ActiveResultSet))
                    _dicResultSet[ActiveResultSet].Encoding = value;
            }
        }

        public int RowCount
        {
            get { 
                return _dicResultSet.ContainsKey(ActiveResultSet)
                            ? _dicResultSet[ActiveResultSet].RowCount
                            : -1; 
            }
        }

        public int ColumnCount
        {
            get {
                return _dicResultSet.ContainsKey(ActiveResultSet)
                        ? _dicResultSet[ActiveResultSet].ColumnCount
                        : -1;
            }
        }

        public object Cells(int iIndex)
        {
            return _dicResultSet.ContainsKey(ActiveResultSet)
                    ? _dicResultSet[ActiveResultSet].Cells(iIndex)
                    : null;
        }

        public object Cells(string strColumnName)
        {
            return _dicResultSet.ContainsKey(ActiveResultSet)
                    ? _dicResultSet[ActiveResultSet].Cells(strColumnName)
                    : null;
        }

        public List<string> ColumnNames()
        {
            return _dicResultSet.ContainsKey(ActiveResultSet)
                    ? _dicResultSet[ActiveResultSet].ColumnNames()
                    : null;
        }

        public List<string> ColumnHeaders()
        {
            return _dicResultSet.ContainsKey(ActiveResultSet)
                    ? _dicResultSet[ActiveResultSet].ColumnHeaders()
                    : null;
        }

        public List<object> GetColumn(int iIndex)
        {
            return _dicResultSet.ContainsKey(ActiveResultSet)
                    ? _dicResultSet[ActiveResultSet].GetColumn(iIndex)
                    : null;
        }

        public List<object> GetColumn(string strColumnName)
        {
            return _dicResultSet.ContainsKey(ActiveResultSet)
                    ? _dicResultSet[ActiveResultSet].GetColumn(strColumnName)
                    : null;
        }

        public void Reset()
        {
            if(_dicResultSet.ContainsKey(ActiveResultSet))
                _dicResultSet[ActiveResultSet].Reset();
        }

        public bool IsSuccess
        {
            get
            {
                return _dicResultSet.ContainsKey(ActiveResultSet)
                    ? _dicResultSet[ActiveResultSet].IsSuccess
                    : false;
            }
            set
            {
                if (_dicResultSet.ContainsKey(ActiveResultSet))
                    _dicResultSet[ActiveResultSet].IsSuccess = value;
            }
        }

        public string ErrorMessage
        {
            get
            {
                return _dicResultSet.ContainsKey(ActiveResultSet)
                    ? _dicResultSet[ActiveResultSet].ErrorMessage
                    : null;
            }
            set
            {
                if (_dicResultSet.ContainsKey(ActiveResultSet))
                    _dicResultSet[ActiveResultSet].ErrorMessage = value;
            }
        }

        public bool IsDoEvents
        {
            get
            {
                return _dicResultSet.ContainsKey(ActiveResultSet)
                    ? _dicResultSet[ActiveResultSet].IsDoEvents
                    : false;
            }
            set
            {
                if (_dicResultSet.ContainsKey(ActiveResultSet))
                    _dicResultSet[ActiveResultSet].IsDoEvents = value;
            }
        }

        public void Close()
        {
            foreach (IResultSet resultSet in _dicResultSet.Values)
                resultSet.Close();
        }

        #endregion
    }
}
